<?php

class Model_Categories {
	
	protected $category_id;
	protected $parent_id = 0;
	protected $expiration_days = 0;
	protected $position = 0;
	protected $enabled = 1;
	protected $icon = '';
	protected $image = '';
	protected $category_order = 0;
	protected $name = '';
	protected $description = '';
	protected $slug = '';
	
	public function __construct(array $options = null) {
		if (is_array ( $options )) {
			$this->setOptions ( $options );
		}
	}
	
	public function __set($name, $value) {
		$method = 'set' . ucfirst ( $name );
		if (('mapper' == $name) || ! method_exists ( $this, $method )) {
			throw new Exception ( 'Invalid content property' );
		}
		$this->$method ( $value );
	}
	
	public function __get($name) {
		$method = 'get' . ucfirst ( $name );
		if (('mapper' == $name) || ! method_exists ( $this, $method )) {
			throw new Exception ( 'Invalid content property' );
		}
		return $this->$method ();
	}
	
	public function setOptions(array $options) {
		$methods = get_class_methods ( $this );
		foreach ( $options as $key => $value ) {
			$method = 'set' . ucfirst ( $key );
			if (in_array ( $method, $methods )) {
				$this->$method ( $value );
			}
		}
	}
	public function getURI() {
		if ($this->getCategory_id () > 0) {
			return Model_Mapper_Categories::newInstance ()->getURI ( $this->getCategory_id () );
		} else {
			return '';
		}
	}
	
	public function getSectionURI(){
	if ($this->getCategory_id () > 0) {
			return Model_Mapper_Categories::newInstance ()->getSectionURI ( $this->getCategory_id () );
		} else {
			return '';
		}
	}
	
	public function getImageURI() {
		return Model_Mapper_Categories::newInstance ()->getImageURI ( $this );
	}
	
	public function getImagePath() {
		return Model_Mapper_Categories::newInstance ()->getImagePath ( $this );
	}
	
	public function deleteImage() {
		$this->setImage ( NULL );
		Model_Mapper_Categories::newInstance ()->save ( $this );
	}
	public function setCategory_id($value) {
		$this->category_id = $value;
	}
	
	public function getCategory_id() {
		return $this->category_id;
	}
	
	public function setParent_id($value) {
		$this->parent_id = $value;
	}
	
	public function getParent_id() {
		return $this->parent_id;
	}
	
	public function setExpiration_days($value) {
		$this->expiration_days = $value;
	}
	
	public function getExpiration_days() {
		return $this->expiration_days;
	}
	
	public function setPosition($value) {
		$this->position = $value;
	}
	
	public function getPosition() {
		return $this->position;
	}
	
	public function setEnabled($value) {
		$this->enabled = $value;
	}
	
	public function getEnabled() {
		return $this->enabled;
	}
	
	public function setIcon($value) {
		$this->icon = $value;
	}
	
	public function getIcon() {
		return $this->icon;
	}
	
	public function setImage($value) {
		$this->image = $value;
	}
	
	public function getImage() {
		return $this->image;
	}
	
	public function setCategory_order($value) {
		$this->category_order = $value;
	}
	
	public function getCategory_order() {
		return $this->category_order;
	}
	
	public function setName($value) {
		$this->name = $value;
	}
	
	public function getName() {
		return $this->name;
	}
	
	public function setDescription($value) {
		$this->description = $value;
	}
	
	public function getDescription() {
		return $this->description;
	}
	
	public function setSlug($value) {
		$this->slug = $value;
	}
	
	public function getSlug() {
		return $this->slug;
	}
	
	public function enable() {
		$this->enabled = 1;
		$this->save ();
	}
	
	public function disable() {
		$this->enabled = 0;
		$this->save ();
	}
	
	public function getRoot() {
		return Model_Mapper_Categories::newInstance ()->getRoot ( $this );
	}
	
	public function hasItems() {
		return Model_Mapper_Categories::newInstance ()->hasItems ( $this );
	}
	
	public function getItems($recursive = false,$args=array()) {
		return Model_Mapper_Categories::newInstance ()->getItems ( $this, $recursive,$args );
	}
	
	public function getItemCount() {
		return Model_Mapper_Categories::newInstance ()->getItemCount ( $this );
	}
	
	public function increaseOrder() {
		Model_Mapper_Categories::newInstance ()->increaseOrder ( $this );
	}
	
	public function decreaseOrder() {
		Model_Mapper_Categories::newInstance ()->decreaseOrder ( $this );
	}
	
	public function order() {
		Model_Mapper_Categories::newInstance ()->order ();
	}
	
	public function hasSubCategories() {
		return Model_Mapper_Categories::newInstance ()->hasSubCateogries ( $this );
	}
	
	public function getSubCategories($array = false, $recursive = false) {
		if ($array) {
			return Model_Mapper_Categories::newInstance ()->getSubCategories ( $this, $recursive );
		} else {
			
			$items = Model_Mapper_Categories::newInstance ()->getSubCategories ( $this, $recursive );
			return Model_Mapper_Categories::newInstance ()->extend ( $items );
		}
	
	}
	public function toRoot() {
		return Model_Mapper_Categories::newInstance ()->toRoot ( $this );
	}
	
	public function tree() {
		return Model_Mapper_Categories::newInstance ()->tree ();
	}
	
	public function toTree() {
		return Model_Mapper_Categories::newInstance ()->toTree ( $this );
	}
	
	public function save() {
		if ($this->name == '') {
			throw new Exception ( 'Category with empty name can not be saved' );
		}
		if ($this->slug == '') {
			$this->slug = sanitizeSlug ( $this->name );
		}
		
		$count = 1;
		while ( checkSlug ( $this->slug ) ) {
			$this->slug = $this->slug . '_' . $count;
			$count ++;
		}
		
		// slug is valid now save the category
		Model_Mapper_Categories::newInstance ()->save ( $this );
	}
	
	public function toArray() {
		return get_object_vars ( $this );
	}

}

